package com.ts.api.module.channel.entity.bo;

import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.reflection.ReflectionUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.exp.ApiMsgException;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 *  用户注册业务实体
 *
 *  @author Pan
 */
@Data
public class UserMappingRegisterBO {

    /** 用户手机号 */
    private String phone;
    /** 用户手机号Md5 */
    private String phoneMd5;
    /** 性别 */
    private String sex;
    /** 用户姓名 */
    private String userName;
    /** 银行卡 */
    private String bankCard;
    /** 身份证 */
    private String idcard;
    /** 城市 */
    private String city;
    /** ip地址 */
    private String ip;

    public UserMappingRegisterBO() {
        super();
    }

    public String getPhoneMd5() {
        if (phoneMd5 == null && ValidParam.isNotEmpty(phone)) {
            phoneMd5 = MD5.create().digestToHex(phone);
        }
        return phoneMd5;
    }

    public void setPhoneMd5(String phoneMd5) {
        this.phoneMd5 = phoneMd5;
    }

    public static UserMappingRegisterBO convertBO(Map<String, String> formMap) throws ApiMsgException {
        UserMappingRegisterBO userMappingRegisterBO = ClassConvertUtils.toBean(formMap, UserMappingRegisterBO.class);

        /** 不存在phoneMd5的请求时候才会进行动态摘要 */
        if (userMappingRegisterBO.getPhoneMd5() == null) {
            userMappingRegisterBO.setPhoneMd5(MD5.create().digestToHex(userMappingRegisterBO.getPhone()));
        }
        return userMappingRegisterBO;
    }

    public static UserMappingRegisterBO convertBOByPhoneMask(Map<String, String> formMap) throws ApiMsgException {
        return ClassConvertUtils.toBean(formMap, UserMappingRegisterBO.class);
    }

    /**
     *  获取注册基本信息字段
     *
     *  @return List
     */
    public List<String> getRegisterFieldNames() {
        List<String> listRegisterFieldName = CollUtils.newArrayList();

        Field[] fields = ReflectionUtils.getFields(this);
        IteratorUtils.array(fields, field -> listRegisterFieldName.add(field.getName()));
        return listRegisterFieldName;
    }
}
